﻿using FanaticUnitity.FfmpegZoomPictureTo300KB.Models;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace FanaticUnitity.FfmpegZoomPictureTo300KB.Helpers
{
    internal static class FfmpegHelper
    {
        private static string _ffmpegPath;

        public static void InitFfmpeg(string ffmpegFilePath)
        {
            _ffmpegPath = ffmpegFilePath;
        }

        public static Size GetImageSize(string filePath)
        {
            CheckFfmpeg();

            var process = new Process();
            var info = process.StartInfo;
            info.FileName = _ffmpegPath;
            info.Arguments = $" -i \"{filePath}\"";
            info.StandardErrorEncoding = Encoding.UTF8;
            info.RedirectStandardOutput = true;
            info.RedirectStandardError = true;
            info.UseShellExecute = false;
            process.Start();

            var reader = process.StandardError;

            var pattern = "[0-9]+x[0-9]+";
            var regex = new Regex(pattern);

            var size = default(Size);

            while (reader.EndOfStream == false)
            {
                var line = reader.ReadLine();

                var matchResult = regex.Match(line);
                if (matchResult.Success)
                {
                    var sizeStr = matchResult.ToString();
                    size = Size.Prase(sizeStr);
                    break;
                }
            }

            reader.Close();
            reader.Dispose();

            return size;
        }

        public static string CompressImage(string filePath)
        {
            CheckFfmpeg();
            var image = new Image(filePath);
            var image300Kb = ImageHelper.Convert300KbImage(image);

            var compressedFileName = FileHelper.GetCompressedFileName(filePath);

            FileHelper.CreateCompressedDirectory(filePath);
            File.Delete(compressedFileName);

            var arg = $"-i \"{filePath}\" -vf scale={image300Kb.Size.Width}:{image300Kb.Size.Height} \"{compressedFileName}\"";

            var process = new Process();
            var info = process.StartInfo;
            info.FileName = _ffmpegPath;
            info.Arguments = arg;
            info.StandardErrorEncoding = Encoding.UTF8;
            info.RedirectStandardOutput = true;
            info.RedirectStandardError = true;
            info.UseShellExecute = false;
            process.Start();

       

            return compressedFileName;
        }

        public static string CompressImage(string filePath, Size size)
        {
            CheckFfmpeg();

            var compressedFileName = FileHelper.GetCompressedFileName(filePath);

            FileHelper.CreateCompressedDirectory(filePath);
            File.Delete(compressedFileName);

            var arg = $"-i \"{filePath}\" -vf scale={size.Width}:{size.Height} \"{compressedFileName}\"";

            var process = new Process();
            var info = process.StartInfo;
            info.FileName = _ffmpegPath;
            info.Arguments = arg;
            info.StandardErrorEncoding = Encoding.UTF8;
            info.RedirectStandardOutput = true;
            info.RedirectStandardError = true;
            info.UseShellExecute = false;
            process.Start();

            return compressedFileName;
        }

        [Obsolete("有问题",true)]
        public static string CompressImageLoop(string filePath)
        {
            var compressedFilePath =  CompressImage(filePath);
            
            while (true)
            {
                if (FileHelper.IsSizeSmallerOrEqual300KB(compressedFilePath)) return compressedFilePath;
                var image = new Image(compressedFilePath);
                var image300Kb = ImageHelper.Convert300KbImage(image);
                CompressImage(filePath, image300Kb.Size);
                Console.WriteLine(image300Kb);
            }
        }

        private static void CheckFfmpeg()
        {
            if (File.Exists(_ffmpegPath)) return;
            throw new FileNotFoundException("无法找到 ffmpeg.exe");
        }

    }
}
